package com.nwork.qingmanhua.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

/**
 * 自定义ImageView
 */
public class CardImageView extends ImageView {
    public CardImageView(Context context) {
        super(context);
    }

    public CardImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CardImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        switch (MeasureSpec.getMode(widthMeasureSpec)) {
            case MeasureSpec.EXACTLY:
                //指定宽高  match_parent 为此种类型
                Log.d("**", "宽度的模式是： + MeasureSpec.EXACTLY");
                break;
            case MeasureSpec.AT_MOST:
                //wrap_content 为此种类型  指定宽高 图片的宽度大于指定的宽高 则实际大小为指定的大小
                int size = MeasureSpec.getSize(widthMeasureSpec);
                Log.d("**", "宽度的模式是：MeasureSpec.AT_MOST");
                break;
            case MeasureSpec.UNSPECIFIED:
                //ListView GridView RecyclerView 传入时 为此 有多高显示多高
                break;


        }
        if (getDrawable() != null) {
            float width = MeasureSpec.getSize(widthMeasureSpec);
            float imgHeight = MeasureSpec.getSize(heightMeasureSpec);
            Log.d("!!", "图片的宽度" + width);
            Log.d("!!", "图片的高度" + imgHeight);
            int height = (int) (width / getDrawable().getIntrinsicWidth() *
                    getDrawable().getIntrinsicHeight());
            //heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
            widthMeasureSpec = MeasureSpec.makeMeasureSpec((int) width, MeasureSpec.AT_MOST);
//            setMeasuredDimension(100, 300);
            //   widthMeasureSpec = 50;
            heightMeasureSpec = 100;
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


}
